<?php

class AdjectivesController extends AppController{
    var $name='Adjectives';
    var $components=array('Auth','Session','RequestHandler');
    var $helper=array('Js','Ajax','Javascript');
    var $uses=array('SuggestedAdjectives','Adjective');
    var $paginate = array(
                            'limit' => 60,
                            'order' => array(
                            'Adjective.title' => 'asc'
)
        
);
    
    
    function beforeFilter(){
        parent::beforeFilter();
        
         
    }
    function admin_index(){
       
       if(!$this->Access->check('Adjective', 'read')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller'=>'users','action'=>'index'));
        } 
        if(isset($this->data)){
            $attributes=explode("\n",$this->data['Adjective']['title']);
            
            $n=0;
            $this->data=array();
            foreach($attributes as $value){
              $data['Adjective'][$n]['title']=ucfirst(trim($value));
            $n++;
            }
        
            $this->Adjective->saveAll($data['Adjective']);
        } 
        $this->data=array();
        $data=$this->paginate('Adjective');
        $this->set('adjectives',$data);
    }
    
    
    function admin_response(){
        
        if ( $this->RequestHandler->isAjax() ) {
   		Configure::write ( 'debug', 0 );
   		$this->autoRender=false;
                $this->layout='Ajax';
                debug($_POST);
                if(!empty($_POST['element_id'])){
                    if($this->Adjective->find('count',array('conditions'=>array('Adjective.title ='=>$_POST['update_value'])))=='0'){
                    $query=mysql_query("UPDATE adjective set `title`='".ucfirst($_POST['update_value'])."' WHERE `id`='".$_POST['element_id']."'");
                    $data=$this->Adjective->find('first',array('conditions'=>array('Adjective.id ='=>$_POST['element_id'])));
                    echo $data['Adjective']['title'];
                    
                    }else{
                        echo $_POST['original_value'];
                    }
                }
                
                
        }
    }
    function admin_delete($id=null){
        if(!$this->Access->check('Adjective', 'read')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller'=>'users','action'=>'index'));
        } 
        if ( $this->RequestHandler->isAjax() ) {
   		Configure::write ( 'debug', 0 );
   		$this->autoRender=false;
                $this->layout='Ajax';
        if(!empty($_POST['id'])){
            $id=$_POST['id'];
             $this->Adjective->delete($id);
             
             echo 'Successfuly Deleted ';
             
             
            
        }
        }else{
            $this->SuggestedAdjectives->delete($id);
            $this->redirect(array('action'=>'suggested'));
        }
    }
    function admin_suggested() {
                if(!$this->Access->check('Adjective', 'read')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller'=>'users','action'=>'index'));
        }
        $this->paginate=array('SuggestedAdjectives'=>array('limit'=>5,'recursive'=>'2'));
        //$adjectives=$this->SuggestedAdjectives->find('all');
        $this->set('adjectives',$this->paginate('SuggestedAdjectives'));
        //01723193206
    }
    function admin_add_suggested($id=null){
        if($id){
            $data=$this->SuggestedAdjectives->find('first',array('conditions'=>array('SuggestedAdjectives.id'=>$id)));
            $insert_in_dictionary=mysql_query("INSERT INTO `adjective` (`title`,`created`) VALUES('".ucfirst($data['SuggestedAdjectives']['title'])."','".$data['SuggestedAdjectives']['created']."')");
            $update=mysql_query("INSERT INTO `subject_adjectives` (`adjective_id`,`user_id`,`subject_id`,`created`) VALUES ('".  mysql_insert_id()."','".$data['SuggestedAdjectives']['user_id']."','".$data['SuggestedAdjectives']['subject_id']."',NOW())");
            $delete_entry=mysql_query("DELETE FROM `suggested_adjectives` WHERE `id`='".$id."'");
            $this->Session->setFlash("Adjective assigned to Subject and added to Adjective dictionary");
            $this->redirect(array('action'=>'suggested'));
        }
    }
    
    function admin_delete_suggested($id){
        if($id){
            if($this->SuggestedAdjectives->delete($id)){
                $this->Session->setFlash('Suggestion Deleted');
                $this->redirect(array('controller'=>'Adjectives','action'=>'suggested'));
            }
        }
    }
}
?>
